<?php
/**
 * This file is form http://findcat.cn
 *
 * @link     https://findcat.cn
 * @email    1476982312@qq.com
 */
namespace App\Http\Controllers\Home;

use Carbon\Carbon;
use App\Models\Tag;
use App\Models\Article;
use App\Models\Website;
use Illuminate\Http\Request;
use App\Models\EveryDayContent;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;

class IndexController extends Controller
{
    /**
     * 首页.
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        $page = $request->page;

        $articles = Article::query()
            ->with(['category'])
            ->where('status', true)
            ->orderBy('is_top', 'asc')
            ->where('id','!=', 7)
            ->orderByDesc('created_at')
            ->paginate(8);

        $articles->page = $page;

        $tags = Tag::query()
            ->limit(5)
            ->orderByDesc('created_at')
            ->get();

        $data = [
            'title'     => '首页',
            'articles'  => $articles,
            'tags'      => $tags,
        ];

        return view('home.index', $data);
    }

    /**
     * 热门文章.
     *
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public function hotArticle()
    {
        //拿出当天的热门文章缓存
        $hotArticle = Cache::get(date('Ymd') . '_hot_article');

        if (empty($hotArticle)) {
            //把热门文章加入缓存
            $hotArticle = Cache::remember(date('Ymd') . '_hot_article', '86400', function () {
                return Article::query()->where('id','!=', 7)->where('status', true)->orderBy('click', 'asc')->limit(10)->get();
            });
        }

        return $hotArticle;
    }

    /**
     * 每日一句.
     *
     * @return null|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object
     */
    public function everyDayContent()
    {
        //拿出每日一句缓存
        $everyDayContent = Cache::get(date('Ymd') . '_every_day_content');

        if (empty($everyDayContent)) {
            //把每日一句加入缓存
            $everyDayContent = Cache::remember(date('Ymd') . '_every_day_content', '86400', function () {
                return EveryDayContent::query()->orderBy('id', 'desc')->first();
            });
        }

        return $everyDayContent;
    }

    /**
     * 网站公告.
     *
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public function websiteAnnouncement()
    {
        //拿出网站公告缓存
        $websiteAnnouncement = Cache::get(date('Ymd') . '_website_announcement');

        if (empty($websiteAnnouncement)) {
            //把网站公告加入缓存
            $websiteAnnouncement = Cache::remember(date('Ymd') . '_website_announcement', '86400', function () {
                return Website::query()->orderBy('sort', 'asc')->limit(5)->get();
            });
        }

        return $websiteAnnouncement;
    }

    /**
     * 访问数自增.
     */
    public function articleClicks(Request $request)
    {
        $userClicks = Cache::get('clicks' . now()->format('Y-m-d', '') . $request->ip . $request->article_id);

        if (empty($userClicks)) {
            Cache::put(
                'clicks' . now()->format('Y-m-d', '') . $request->ip . $request->article_id,
                $request->ip . $request->article_id,
                Carbon::tomorrow()
            );
            $article = Article::query()->where('id', $request->article_id)->firstOrFail();

            Article::query()->update(['click' => ++$article->click]);
        }
    }
}
